package
{
	import away3d.containers.View3D;
	import away3d.core.managers.Stage3DManager;
	import away3d.core.managers.Stage3DProxy;
	import away3d.events.Stage3DEvent;
	import com.grouuu.core.SceneComposite;
	import flash.desktop.NativeApplication;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.ui.Multitouch;
	import flash.ui.MultitouchInputMode;
	import starling.core.Starling;
	
	/**
	 * ...
	 * @author Grouuu
	 */
	public class Main extends Sprite
	{
		protected var stage3DManager:Stage3DManager;
		static public var stage3DProxy:Stage3DProxy;
		static public var awayView:View3D;
		static public var starling:Starling;
		
		public function Main():void
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			stage.addEventListener(Event.DEACTIVATE, deactivate);
			
			Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
			
			init();
		}
		
		private function init():void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			stage3DManager = Stage3DManager.getInstance(stage);
			stage3DProxy = stage3DManager.getFreeStage3DProxy();
			stage3DProxy.addEventListener(Stage3DEvent.CONTEXT3D_CREATED, onContextCreated);
			stage3DProxy.antiAlias = 8;
			stage3DProxy.color = 0x0;
		}
		
		protected function onContextCreated(event:Stage3DEvent):void
		{
			awayView = new View3D();
			awayView.stage3DProxy = stage3DProxy;
			awayView.shareContext = true;
			
			addChild(awayView);
			
			starling = new Starling(SceneComposite, stage, stage3DProxy.viewPort, stage3DProxy.stage3D);
			starling.antiAliasing = 1;
			starling.start();
		}
		
		protected function deactivate(event:Event):void
		{
			NativeApplication.nativeApplication.exit();
		}
	}
}